package com.qtenv.datasync.utils;

import java.io.*;

/**
 * @ClassName test
 * @Author CBL
 * @Date 2022/10/12 15:22
 **/
public class test {
//    private static int text = 1;

    public static void main(String[] args) throws IOException {
        String sumString = readFile(new File("C:/Users/1/Desktop/12.txt"),"000000");
//        String sumString = readFile(new File("C:/Users/Admin/Desktop/地图数据2/新安江(1).txt"),"403532");
//        String sumString = readFile(new File("C:/Users/Admin/Desktop/地图数据2/梅城(1).txt"),"403532");


//        //前后最处理
//        sumString = "insert into test(Column1,Column2,Column3) values\n" + sumString;
//        StringBuilder sb = new StringBuilder(sumString);
//        sb.replace(sumString.length()-1,sumString.length(),";");
//        sumString = sb.toString();
        System.out.println(sumString);

        //创建文件
        BufferedWriter out = new BufferedWriter(new FileWriter("runoob.txt"));
        out.write(sumString);
        out.close();



    }




    //读取文件
    static String readFile(File file,String locationArea){
        BufferedReader reader = null;
        String tempString = null;
        String sumString = new String();
        int line = 1;
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            while ((tempString = reader.readLine()) != null) {
                if (tempString.length() >= 8 && tempString.substring(0,8).indexOf(locationArea) != -1) sumString += "\n";
                if (tempString.equals("")) continue;
//                System.out.println("Line" + line + ":" + tempString);
                tempString = disposeFile(tempString);
                tempString = splitString(tempString);
                sumString += tempString;
//                line++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        sumString = sumString.trim();
        return sumString;
    }

    static String disposeFile(String lineString) {
        StringBuilder stringBuilder = new StringBuilder(lineString);
        lineString = "";
        for (int i = 0; i < stringBuilder.length(); i++) {

            if (stringBuilder.charAt(i) == '[') {
                lineString += "update dw_zone set AreaPoint = '" + stringBuilder.charAt(i);
                i++;
                while (i < stringBuilder.length() && stringBuilder.charAt(i) != ']' || (  (i+1) < stringBuilder.length() && stringBuilder.charAt(i + 1) != ']' ) ) {
                    if (stringBuilder.charAt(i) == ']') {
                        lineString += "}";
                    } else if (stringBuilder.charAt(i) == '[') {
                        lineString += "{lng:";
                    } else if (stringBuilder.charAt(i) == ','){
                        if ( (i + 1) < stringBuilder.length() && stringBuilder.charAt(i+1) == '['){
                            lineString += ",";
                        }else {
                            lineString += ",lat:";
                        }
                    } else {
                        lineString += stringBuilder.charAt(i);
                    }
                    i++;
                }
                lineString += "}]'";
                i++;
                break;
            }

            if (stringBuilder.charAt(i) == ',') {
                lineString += ",";
            } else {
                lineString += stringBuilder.charAt(i);
            }
        }
        lineString += ",";
        return lineString;
    }

    static String splitString(String lineString){
        String[] str = new String[3];
//        System.out.println(lineString);
        str = lineString.split(",",3);


        //后最处理
        StringBuilder sb = new StringBuilder(str[2]);
        sb.replace(sb.length()-1,sb.length(),"");
        str[2] = sb.toString();

        lineString = str[2] + " where" + " tenant_id = '" + str[0] + "' and " + "AreaName = '" + str[1] + "';";

        return lineString;
    }



}
